package com.ethan.blog.common.aspect;

import com.ethan.blog.common.annotation.AutoFill;
import com.ethan.blog.common.constant.AutoFillConstant;
import com.ethan.blog.common.constant.StatusCodeConstant;
import com.ethan.blog.common.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author: Ethan
 * @Time: 2024/5/26 10:01
 * @description: 自填充共享信息信息
 */
@Aspect
@Component
@Slf4j// 日志注解
public class AutoFillAspect {
    /**
     * 自动填充切面
     */
    @Pointcut("(execution(* com.ethan.blog.mapper.*.*(..)) || execution(* com.ethan.blog.service.*.*(..))) " +
            "&& @annotation(com.ethan.blog.common.annotation.AutoFill)")
    public void autoFillPointCut() {
    }

    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint) {
        log.info("自动切面执行，开始填充共享信息");
        // 获取当前拦截器的方法上数据库操作类型
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();// 获取方法签名
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);// 获取方法上的注解
        OperationType operationType = autoFill.value();// 获取注解上的值

        // 获取传入方法的实参 -- 实体对象
        Object[] args = joinPoint.getArgs();
        if (args == null || args.length == 0) {
            return;
        }

        // 获取实体对象
        Object entity = args[0];

        // 准备赋值数据
        Date nowDate = new Date();
        if (operationType == OperationType.INSERT) {
            try {
                entity.getClass().getDeclaredMethod(AutoFillConstant.SET_PERMISSION_LEVEL, Integer.class).invoke(entity, StatusCodeConstant.AUTHORITY_LEVEL_USER);
                entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_ID, Long.class).invoke(entity, StatusCodeConstant.USER_BY_ID);
                entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, Date.class).invoke(entity, nowDate);
                entity.getClass().getDeclaredMethod(AutoFillConstant.SET_DELETED, Integer.class).invoke(entity, StatusCodeConstant.IS_DELETE_NO);

                entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_ID, Long.class).invoke(entity, StatusCodeConstant.USER_BY_ID);
                entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, Date.class).invoke(entity, nowDate);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (operationType == OperationType.UPDATE) {
            try {
                entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_ID, Long.class).invoke(entity, StatusCodeConstant.USER_BY_ID);
                entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, Date.class).invoke(entity, nowDate);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
